package com.my.xq.service.impl;

import java.util.List;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.my.xq.domain.ProprietorAuthParam;
import com.my.xq.domain.entity.Proprietor;
import com.my.xq.domain.entity.ProprietorAuth;
import com.my.xq.domain.vo.ProprietorAuthInfoVO;
import com.my.xq.repository.ProprietorAuthRepository;
import com.my.xq.repository.ProprietorRepository;

import io.swagger.annotations.ApiOperation;

@RestController
@RequestMapping("/door/auth")
public class ProprietorAuthServiceImpl {
	@Autowired
	ProprietorAuthRepository proprietorAuthRepository;

	@Autowired
	ProprietorRepository proprietorRepository;

	@CrossOrigin(origins = "*", maxAge = 3600)
	@RequestMapping(value = "/isauth", method = { RequestMethod.GET })
	@ApiOperation(value = "查询是否被授权", notes = "查询是否被授权", httpMethod = "GET")
	public boolean isAuth(String doorId) {
		String isAuth = proprietorAuthRepository.isAuth(doorId);
		return "true".equals(isAuth);
	}

	@CrossOrigin(origins = "*", maxAge = 3600)
	@RequestMapping(value = "/info", method = { RequestMethod.GET })
	@ApiOperation(value = "根据业主id和门牌号查询是否被授权", notes = "查询是否被授权", httpMethod = "GET")
	public ProprietorAuthInfoVO authInfo(String doorId, String pid) {
		ProprietorAuthInfoVO rtn = new ProprietorAuthInfoVO();
		ProprietorAuth auth = proprietorAuthRepository.findByDoorId(doorId);
		if (auth != null && pid != null) {
			if (pid.equals(auth.getAuthorizer())) {
				rtn.setAuthorizer(true);
			} 
			if (pid.equals(auth.getProprietorId())) {
				rtn.setAuthorized(true);
			}
		}
		return rtn;
	}

	@CrossOrigin(origins = "*", maxAge = 3600)
	@RequestMapping(value = "/findOneByDoorId", method = { RequestMethod.GET })
	@ApiOperation(value = "根据门牌号查询授权", notes = "根据门牌号查询授权", httpMethod = "GET")
	public ProprietorAuthParam findOneByDoorId(String doorId) {
		ProprietorAuthParam rtn = new ProprietorAuthParam();
		List<Object[]> rs = proprietorAuthRepository.findProprietorAndAuthByDoorId(doorId);
		if (rs != null && rs.size() > 0) {
			Object[] rsArr = rs.get(0);
			rtn.setProprietorAuthId(String.valueOf(rsArr[0]));
			rtn.setAuthorizer(String.valueOf(rsArr[1]));
			rtn.setDoorplateId(String.valueOf(rsArr[2]));
			rtn.setProprietorId(String.valueOf(rsArr[3]));
			rtn.setAuth(Boolean.valueOf(String.valueOf(rsArr[4])));
			rtn.setName(String.valueOf(rsArr[5]));
			rtn.setIdNo(String.valueOf(rsArr[6]));
			rtn.setPhone(String.valueOf(rsArr[7]));
			rtn.setGender(Integer.valueOf(String.valueOf(rsArr[8])));
		}
		return rtn;
	}

	@CrossOrigin(origins = "*", maxAge = 3600)
	@RequestMapping(value = "/save", method = { RequestMethod.POST })
	@ApiOperation(value = "业主投票权转授", notes = "业主投票权转授", httpMethod = "POST")
	public boolean save(ProprietorAuthParam param) {
		try {
			ProprietorAuth entity = new ProprietorAuth();
			if (StringUtils.isEmpty(param.getProprietorId())) {
				Proprietor p = new Proprietor();
				p.setIdentityCard(param.getIdNo());
				p.setName(param.getName());
				p.setPhone(param.getPhone());
				p.setGender(param.getGender());
				p.setPriType("租客");
				proprietorRepository.save(p);
				entity.setProprietorId(p.getId());
			} else {
				entity.setProprietorId(param.getProprietorId());
			}
			entity.setAuthorizer(param.getAuthorizer());
			entity.setAuth(param.isAuth());
			entity.setDoorplateId(param.getDoorplateId());
			proprietorAuthRepository.save(entity);
			return true;
		} catch (Exception e) {
			return false;
		}
	}

	@CrossOrigin(origins = "*", maxAge = 3600)
	@RequestMapping(value = "/del", method = { RequestMethod.POST })
	@ApiOperation(value = "业主投票权撤销授权", notes = "业主投票权撤销授权", httpMethod = "POST")
	public boolean del(String id) {
		try {
			ProprietorAuth entity = proprietorAuthRepository.get(id);
			if (entity != null) {
				entity.setAuth(false);
				entity.setDelFlag(true);
				proprietorAuthRepository.save(entity);
			}
			return true;
		} catch (Exception e) {
			return false;
		}
	}
}
